<?php

    class Owners_model extends CI_Model{

        var $page_TYPES = array('page','arrangement');
        private $fields = array('post_title','content');

        function __construct(){
            parent::__construct();
        }

        /* @params
        * 
        * 
        accounting_code    
        address    Jovana Tomaševića
        bank_ac_number    530-20322-60
        bank_name    NLB Montenegro
        birthdate    01.08.1982
        birthplace    Banjaluka
        citizenship    montenegrin
        city    Bar
        company_person    company
        contract_nr    001-12
        contract_type    1 year
        country    Montenegro
        default_option_period    
        due_date    
        email    ikukic@yahoo.com
        language    Serbian
        mobile_phone    069069982
        name    ivan
        owner_type[]    customer
        pasport_number    AC37B6V
        personal_ID    123456
        post_action    NEW
        post_type    owner
        sex    male
        surname    kukic
        swift    u38s00sj
        tax_type    Insurance
        telefax    312324
        telephone    312324
        telephone2    312324
        zip_code    85000
        */

        function create_owner(){

            unset($_POST['post_action']);
            unset($_POST['post_type']);

            /*Parse data*/
            $_POST['customer'] = in_array('customer', $_POST['owner_type']) ? TRUE : FALSE;
            $_POST['suplier'] = in_array('suplier', $_POST['owner_type']) ? TRUE : FALSE;
            $_POST['partner'] = in_array('partner', $_POST['owner_type']) ? TRUE : FALSE;            
            unset($_POST['owner_type']);

            $_POST['birthdate'] = strtotime($_POST['birthdate']);


            $this->db->insert('owners', $_POST);

            if($this->db->affected_rows()>0){
                return array('action'=>true,'msg'=>'Successful insert operation.');
            }else{
                return array('action'=>false,'msg'=>'Something went wrong. Please try again.');
            }

        }

        function update_post(){             

            $ID = $_POST['post_ID'];

            unset($_POST['post_ID']);
            unset($_POST['post_action']);
            unset($_POST['post_type']);

            /*Parse data*/
            $_POST['customer'] = in_array('customer', $_POST['owner_type']) ? TRUE : FALSE;
            $_POST['suplier'] = in_array('suplier', $_POST['owner_type']) ? TRUE : FALSE;
            $_POST['partner'] = in_array('partner', $_POST['owner_type']) ? TRUE : FALSE;            
            unset($_POST['owner_type']);

            $_POST['birthdate'] = strtotime($_POST['birthdate']);

            $this->db->where('ID',$ID);
            $this->db->update('owners', $_POST);

            if($this->db->affected_rows()>=0){
                return array('action'=>true,'msg'=>'Successful edit operation.');
            }else{
                return array('action'=>false,'msg'=>'The Owners data is same as before.');
            }

        }

        function delete_owner($post_ID){

            // First Unlink gallery
            /*$res = $this->db->get_where('gallery',array('posts_ID'=>$post_ID))->row_array();

            if(isset($res['ID'])){
            $this->db->where('ID',$res['ID']);
            $this->db->update('gallery',array('posts_ID'=>NULL)); 
            }

            // Unlink documents second
            $res = $this->db->get_where('folders',array('posts_ID'=>$post_ID))->row_array();
            if(isset($res['ID'])){
            $this->db->where('ID',$res['ID']);
            $this->db->update('folders',array('posts_ID'=>NULL));
            }*/ 

            //Delete post
            $this->db->where('ID',$post_ID);
            $this->db->delete('owners');

            if($this->db->affected_rows()>0){
                return TRUE;
            }else{
                return TRUE;
            }

        }

        function view_all_owners(){

            if(!isset($_GET['post_type']))$_GET['post_type']='owner';

            $res = $this->db->get('owners')->result_array();

            return $res;

        }

        function view_edit_owner($ID){

            $this->db->where('ID',$ID);
            $res = $this->db->get('owners')->row_array();
            return $res;

        }

        function view_related_post($post_TYPE){ // Page, arrangements, cars etc...

            $this->db->where('post_status','publish');
            $this->db->where('post_type',$post_TYPE);
            $this->db->where('parent_id',0);
            RETURN $this->db->get('posts')->result_array();

        }

        function get_page_types(){
            RETURN $this->page_TYPES;
        }

        function get_fields(){
            RETURN $this->fields;
        }

    }

?>
